package chpt10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SelectionDemo extends JFrame {
    private JCheckBox checkBox1;
    private JCheckBox checkBox2;
    private JCheckBox checkBox3;
    private JRadioButton radioButton1;
    private JRadioButton radioButton2;
    private JRadioButton radioButton3;
    private JRadioButton radioButton4;
    private ButtonGroup buttonGroup;
    private JComboBox<String> comboBox;

    public SelectionDemo() {
        setTitle("选择组件示例");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLayout(new FlowLayout());

        JPanel checkBoxPanel = new JPanel();
        checkBoxPanel.setBorder(BorderFactory.createTitledBorder("你想吃什么？"));
        checkBox1 = new JCheckBox("面条");
        checkBox2 = new JCheckBox("稀饭");
        checkBox3 = new JCheckBox("白米饭");
        checkBoxPanel.add(checkBox1);
        checkBoxPanel.add(checkBox2);
        checkBoxPanel.add(checkBox3);
        add(checkBoxPanel);

        JPanel radioButtonPanel = new JPanel();
        radioButtonPanel.setBorder(BorderFactory.createTitledBorder("底下选项哪个错？"));
        radioButton1 = new JRadioButton("A. 1+1 = 2");
        radioButton1.setActionCommand("A");
        radioButton2 = new JRadioButton("B. 1+2 = 3");
        radioButton2.setActionCommand("B");
        radioButton3 = new JRadioButton("C. 1+3 = 4");
        radioButton3.setActionCommand("C");
        radioButton4 = new JRadioButton("D. 1+1 = 5");
        radioButton4.setActionCommand("D");
        buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        buttonGroup.add(radioButton3);
        buttonGroup.add(radioButton4);
        radioButtonPanel.add(radioButton1);
        radioButtonPanel.add(radioButton2);
        radioButtonPanel.add(radioButton3);
        radioButtonPanel.add(radioButton4);
        add(radioButtonPanel);
        
        JPanel comboBoxPanel = new JPanel();
        comboBox = new JComboBox<>(new String[]{"飞机", "高铁", "大巴"});
        comboBoxPanel.setBorder(BorderFactory.createTitledBorder("乘坐"));
        comboBoxPanel.add(comboBox);
        comboBox.setSelectedIndex(1); // 设置默认选中项
        add(comboBoxPanel);

        JButton button = new JButton("提交");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedCheckBoxes = "选择的复选框选项:";
                if (checkBox1.isSelected()) {
                	selectedCheckBoxes += "面条";
                }
                if (checkBox2.isSelected()) {
                	selectedCheckBoxes += "稀饭";
                }
                if (checkBox3.isSelected()) {
                	selectedCheckBoxes += "白米饭";
                }
                
                String selectedRadioButton = "选择的单选框选项:";
                ButtonModel selectedButtonModel = buttonGroup.getSelection();
                if (selectedButtonModel != null) {
                    String selectedOption = selectedButtonModel.getActionCommand();
                    selectedRadioButton += selectedOption;
                } else {
                    selectedRadioButton += "未选择";
                }
                

                String selectedComboBox = "选择的下拉框选项: " + comboBox.getSelectedItem();

                JOptionPane.showMessageDialog(null, selectedCheckBoxes + "\n" + selectedRadioButton + "\n" + selectedComboBox);
            }
        });
        add(button);

        setVisible(true);
    }

    public static void main(String[] args) {
        new SelectionDemo();
    }
}